//
//  LZReviewTableViewCellReplyView.m
//  laziz_Merchant
//
//  Created by xbn_iMac on 2017/3/1.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZReviewTableViewCellReplyView.h"
#import "LZDateFormatterManager.h"
#import "Masonry.h"

@interface LZReviewTableViewCellReplyView ()

@property (nonatomic, strong) UIImageView *replyImageView;

@property (nonatomic, strong) UILabel *labReplyTitle;
@property (nonatomic, strong) UILabel *labReplyTime;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UILabel *labReplyContent;

@property (nonatomic, assign) BOOL isDeletedReply;

@end

@implementation LZReviewTableViewCellReplyView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor clearColor];
    
    [self configSubviews];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
}

- (void)setReplyText:(NSString *)replyText andReplyTime:(NSString *)replyTime {
    
    if (self.isMessageCenter) {
        self.replyImageView.hidden = NO;
        if (replyText.length > 0) {
            self.labReplyContent.text = replyText;
            self.labReplyContent.textColor = UIColorFromRGB(0x666666, 1);

            self.labReplyTime.text = [[LZDateFormatterManager shareInstance] stringFromStringNormalHaveLineyyyyMMdd:replyTime];

            self.isDeletedReply = NO;
            self.labReplyTitle.hidden = NO;
            self.labReplyTime.hidden = NO;
            self.line.hidden = NO;
            
            [self.labReplyContent mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(10);
                make.top.equalTo(self.line.mas_bottom).offset(10);
                make.trailing.equalTo(-10);
            }];
        }
        else {
            self.labReplyContent.text = LZLocalizedString(@"review_reply_deleted_remind", @"");
            self.labReplyContent.textColor = UIColorFromRGB(0xfa5a5a, 1);

            self.isDeletedReply = YES;
            self.labReplyTitle.hidden = YES;
            self.labReplyTime.hidden = YES;
            self.line.hidden = YES;
            
            [self.labReplyContent mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(10);
                make.top.equalTo(self.labReplyTitle.mas_top);
                make.trailing.equalTo(-10);
            }];
        }
    }
    else {
        self.hidden = (replyText.length == 0);
        self.labReplyContent.text = replyText;
    }
}


- (void)configSubviews {
    self.replyImageView = [[UIImageView alloc] init];
    [self addSubview:self.replyImageView];
    
    UIImage *image = ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) ? [UIImage imageNamed:@"reviews_reply_text_bg_ar"] : [UIImage imageNamed:@"reviews_reply_text_bg_en"];
    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)];
    self.replyImageView.image = image;
    
    self.labReplyTitle = [[UILabel alloc] init];
    self.labReplyTitle.textColor = UIColorFromRGB(0x666666, 1);
    self.labReplyTitle.text = LZLocalizedString(@"review_reply", @"");
    self.labReplyTitle.font = [UIFont systemFontOfSize:13];
    [self.replyImageView addSubview:self.labReplyTitle];
    
    
    self.labReplyTime = [[UILabel alloc] init];
    self.labReplyTime.textColor = UIColorFromRGB(0x999999, 1);
    self.labReplyTime.font = [UIFont systemFontOfSize:11];
    [self.replyImageView addSubview:self.labReplyTime];
    
    
    self.line = [[UIView alloc] init];
    self.line.backgroundColor = UIColorFromRGB(0xffffff, 1);
    [self.replyImageView addSubview:self.line];
    
    
    self.labReplyContent = [[UILabel alloc] init];
    self.labReplyContent.textColor = UIColorFromRGB(0x666666, 1);
    self.labReplyContent.font = [UIFont systemFontOfSize:13];
    self.labReplyContent.numberOfLines = 0;
    self.labReplyContent.preferredMaxLayoutWidth = SCREEN_WIDTH-79-20;
    [self.replyImageView addSubview:self.labReplyContent];
    
    [self configVFL];
}

- (void)configVFL {
    [self.replyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.leading.and.trailing.equalTo(0);
        make.bottom.equalTo(self.labReplyContent.mas_bottom).offset(10);
    }];
    
    [self.labReplyTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(10);
        make.top.equalTo(12.5);
    }];
    
    [self.labReplyTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(-10);
        make.centerY.equalTo(self.labReplyTitle);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(10);
        make.trailing.equalTo(-10);
        make.top.equalTo(self.labReplyTitle.mas_bottom).offset(10);
        make.height.equalTo(0.5);
    }];
    
    [self.labReplyContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(10);
        make.top.equalTo(self.line.mas_bottom).offset(10);
        make.trailing.equalTo(-10);
    }];
}

- (CGSize)intrinsicContentSize {
    if (self.labReplyContent.text.length>0) {
        if (self.isDeletedReply) {
            return CGSizeMake(0, 12.5 +  self.labReplyContent.intrinsicContentSize.height + 10);
        }
        else {
            return CGSizeMake(0, 12.5 + self.labReplyTitle.intrinsicContentSize.height + 10 + 0.5 + 10 +  self.labReplyContent.intrinsicContentSize.height + 10);
        }
    }
    else {
        return CGSizeMake(0, 0);
    }
}

@end
